package gluck.sintactico.tree;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Node {
	protected String name;
	protected List<Node> nodes;
	
	public Node(String name){
		this.name= name;
		this.nodes=null;
	}
	
	public void addNode(String name){
		addNode(new Node(name));
	}
	
	public void addNode(Node node){
		if(this.nodes==null){
			this.nodes= new ArrayList<Node>();
		}
		nodes.add(node);
	}
	
	public void addNode(Object node){
		if(node!=null)
		{
			addNode((Node)node);
		}
	}
	
	
	public String getName(){
		return this.name;
	}
	
	public int printNodes(int i){
		System.out.println("Node"+i+ " [label=\""+this.name+"\"];");
		int j=i+1;
		if(this.nodes!=null){
			for(Node n: this.nodes){
				System.out.println("Node"+i+ " -> " + "Node"+j+";");
				j+=n.printNodes(j);
			}
		}
		return j-i;
	}
	
	protected int printNodesInFile(FileWriter file, int i) throws IOException{
		file.write("Node"+i+ " [label=\""+this.name.replaceAll("\"", "'")+"\"];\n");
		int j=i+1;
		if(this.nodes!=null){
			for(Node n: this.nodes){
				file.write("Node"+i+ " -> " + "Node"+j+";\n");
				j+=n.printNodesInFile(file, j);
			}
		}
		return j-i;
	}
}
